﻿using System;
using System.Windows;
using gp.BiztalkHelpers.PipelinesReferenceViewer.DAL;
using gp.BiztalkHelpers.PipelinesReferenceViewer.IhmModel;
using gp.BiztalkHelpers.PipelinesReferenceViewer.Model;
using gp.BiztalkHelpers.PipelinesReferenceViewer.Properties;

namespace gp.BiztalkHelpers.PipelinesReferenceViewer
{
	/// <summary>
	/// Logique d'interaction pour MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		private DAO _daoInstance;
		private string _connectionString;

		public MainWindow()
		{
			InitializeComponent();

			this.InitializeConnectionString();
			this._daoInstance = DAO.GetInstance(this._connectionString);

			this.PopulateComboTypes();
		}

		#region private methods
		private void PopulateComboTypes() {
			var rcvPipeline = new ComboboxItem()
			{
				Text = "Receive Pipeline",
				Value = PipelineCategory.Receive
			};

			var sndPipeline = new ComboboxItem()
			{
				Text = "Send Pipeline",
				Value = PipelineCategory.Send
			};

			this.cmbPpCategory.Items.Add(rcvPipeline);
			this.cmbPpCategory.Items.Add(sndPipeline);
		}

		private void InitializeConnectionString()
		{
			this._connectionString = Settings.Default.MgmtDB;
			this.tbkConnection.Text = this._connectionString;
		}

		private void CleanLbxResult() {
			this.lbxRcvLoc.ItemsSource = null;
			this.lbxSndPorts.ItemsSource = null;
		}
		#endregion

		#region events
		private void btnSearchPipelines_Click(object sender, RoutedEventArgs e) {
			if (this.cmbPpCategory.SelectedItem != null) {
				var selectedPpCateg = (PipelineCategory)(this.cmbPpCategory.SelectedItem as ComboboxItem).Value;
				try
				{
					this.lbxPipelines.ItemsSource = this._daoInstance.GetAllPipelines(selectedPpCateg);
				}
				catch (Exception ex)
				{
					MessageBox.Show("An error occurred during database querying. See details for more informations : " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
				}

				this.CleanLbxResult();
			}
		}

		private void btnGetPipelinesReferences_Click(object sender, RoutedEventArgs e) {
			if (this.cmbPpCategory.SelectedItem != null && this.lbxPipelines.SelectedItem != null) {
				var selectedPpCateg = (PipelineCategory)(this.cmbPpCategory.SelectedItem as ComboboxItem).Value;
				var pplId = (this.lbxPipelines.SelectedItem as Pipeline).Id;

				try
				{
					switch (selectedPpCateg)
					{
						case PipelineCategory.Receive:
							this.lbxRcvLoc.ItemsSource = this._daoInstance.GetAllRcvLocationsFromRcvPipeline(pplId);
							this.lbxSndPorts.ItemsSource = this._daoInstance.GetAllSndPortsFromRcvPipeline(pplId);
							break;

						case PipelineCategory.Send:
							this.lbxRcvLoc.ItemsSource = this._daoInstance.GetAllRcvLocationsFromSndPipeline(pplId);
							this.lbxSndPorts.ItemsSource = this._daoInstance.GetAllSndPortsFromSndPipeline(pplId);
							break;

						default:
							this.CleanLbxResult();
							break;
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show("An error occurred during database querying. See details for more informations : " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
				}
			}
		}

		private void btnChangeConnection_Click(object sender, RoutedEventArgs e)
		{

		}
		#endregion
	}
}
